using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;

using Framework;

namespace SchemaMonitor
{
    [Serializable()]
    public partial class CUserList : List<CUser>
    {
        #region Constructors
        //Basic constructor
        public CUserList() : base() {}
       
        //More efficient memory-allocation if size is known
        public CUserList(int capacity) : base(capacity) {}
        
        //Shares the index (if its already been computed)
        public CUserList(CUserList list) : base(list.Count)
        {
            foreach (CUser i in list)
                base.Add(i);
        }

        //Generic list (eg. from paging control), have to assume type
        public CUserList(IList list) : base(list.Count)
        {
            foreach (CUser i in list)
                base.Add(i);
        }
        #endregion
        
        #region Top/Bottom/Page
        public CUserList Top(int count)
        {
            if (count >= this.Count)
                return this;
            return Page(count, 0); 
        }
        public CUserList Bottom(int count)   
        {
            if (count > this.Count)
                count = this.Count;
            return new CUserList(this.GetRange(this.Count - count - 1, count));
        }
        public CUserList Page(int pageSize, int pageIndex)
        {
            return new CUserList( CUtilities.Page(this, pageSize, pageIndex) );
        }
        #endregion

        #region BulkEditLogic
        public bool HaveSameValue(string propertyName)               { return CReflection.HaveSameValue(this, propertyName); }
        public void SetSameValue( string propertyName, object value) {        CReflection.SetSameValue( this, propertyName, value); }
        #endregion

        #region SortBy
        //Public
        public CUserList SortBy(string propertyName) { return SortBy(propertyName, false); }
        public CUserList SortBy(string propertyName, bool descending)
        {
            CUserList copy = new CUserList(this);
            if (this.Count == 0)    return copy;
            copy.Sort(new CUserList_SortBy(propertyName, descending, this));
            return copy;
        }
        //Private 
        private class CUserList_SortBy : CReflection.GenericSortBy, IComparer<CUser>
        {
            public CUserList_SortBy(string propertyName, bool descending, IList list) : base(propertyName, descending, list) { }
            public int Compare(CUser x, CUser y) { return base.Compare(x, y); }
        }
        #endregion

        #region SaveAll/DeleteAll
        //Use default connection (may be overridden in base class)
        public void SaveAll()   {   if (this.Count > 0) { SaveAll(  this[0].DataSrc);    }    }
        public void DeleteAll() {   if (this.Count > 0) { DeleteAll(this[0].DataSrc);    }    }

        //Use connection supplied
        public void SaveAll(  CDataSrc dataSrc) {   dataSrc.BulkSave(this);    }
        public void DeleteAll(CDataSrc dataSrc) {   dataSrc.BulkDelete(this);  }

        //Use transaction supplied
        public void SaveAll(  IDbTransaction txOrNull)    {   foreach (CUser i in this) {   i.Save(  txOrNull);   }   }
        public void DeleteAll(IDbTransaction txOrNull)    {   foreach (CUser i in this) {   i.Delete(txOrNull);   }   }

        //Use a specified isolation level
        public void SaveAll(  IsolationLevel txIsolationLevel)   {   if (this.Count > 0) { SaveAll(  this[0].DataSrc, txIsolationLevel);  }    }
        public void DeleteAll(IsolationLevel txIsolationLevel)   {   if (this.Count > 0) { DeleteAll(this[0].DataSrc, txIsolationLevel);  }    }

        //Use a specified connection and isolation level
        public void SaveAll(  CDataSrc dataSrc, IsolationLevel txIsolationLevel)   {   dataSrc.BulkSave(  this, txIsolationLevel);  }
        public void DeleteAll(CDataSrc dataSrc, IsolationLevel txIsolationLevel)   {   dataSrc.BulkDelete(this, txIsolationLevel);  }
        #endregion

        #region List of Ids
        List<long> _ids;
        public List<long> Ids
        {
            get
            {
                if (null == _ids)
                {
                    List<long> ids = new List<long>(this.Count);
                    foreach (CUser i in this)
                        ids.Add(i.UserId);
                    _ids = ids;
                }
                return _ids;
            }
        }
        public CUserList GetByIds(List<long> ids)
        {
            CUserList list = new CUserList(ids.Count);
            foreach (long id in ids)
                if (null != GetById(id))
                    list.Add(GetById(id));
            return list;
        }
        #endregion
        
        #region Cache-Control
        //Main Logic
        public new void Add(CUser item)
        {
            if (null != _index && ! _index.ContainsKey(item.UserId))
                _index[item.UserId] = item;
            base.Add(item);
        }
        public new void Remove(CUser item)
        {
            if (null != _index && _index.ContainsKey(item.UserId))
                _index.Remove(item.UserId);
            base.Remove(item);
        }
    
        //Supplementary List Overloads
        public void Add(   IList<CUser> itemsToAdd)    {   foreach (CUser i in itemsToAdd)   { Add(   i); }   }
        public void Remove(IList<CUser> itemsToRemove) {   foreach (CUser i in itemsToRemove){ Remove(i); }   }
        #endregion
        
        #region Main Index (on UserId)
        public CUser GetById(long userId)
        {
            CUser c = null;
            Index.TryGetValue(userId, out c);
            return c;
        }
        [NonSerialized]
        private Dictionary<long,CUser> _index;
        private Dictionary<long,CUser> Index
        {
            get
            {
                if (null != _index)
                    if (_index.Count == this.Count)
                        return _index;

                    _index = new Dictionary<long,CUser>(this.Count);
                        foreach (CUser i in this) 
                            _index[i.UserId] = i;
                return _index;
            }
        }
        #endregion
            
        #region Foreign-Key Indices (Subsets)
        //Index by UserSchemaId
        public CUserList GetBySchemaId(int schemaId)
        {
            CUserList temp = null;
            if (! IndexBySchemaId.TryGetValue(schemaId, out temp))
            {
                temp = new CUserList();
                IndexBySchemaId[schemaId] = temp;
            }
            return temp;
        }

        [NonSerialized]
        private Dictionary<int, CUserList> _indexBySchemaId;
        private Dictionary<int, CUserList> IndexBySchemaId
        {
            get
            {
                if (null == _indexBySchemaId)
                {
                    Dictionary<int, CUserList> index = new Dictionary<int, CUserList>();
                    CUserList temp = null;
                    foreach (CUser i in this)
                    {
                        if (! index.TryGetValue(i.UserSchemaId, out temp))
                        {
                            temp = new CUserList();
                            index[i.UserSchemaId] = temp;
                        }
                        temp.Add(i);
                    }
                    _indexBySchemaId = index;
                }
                return _indexBySchemaId;
            }
        }
        //Index by UserNodeGuid
        public CUserList GetByNodeGuid(Guid nodeId)
        {
            CUserList temp = null;
            if (! IndexByNodeGuid.TryGetValue(nodeId, out temp))
            {
                temp = new CUserList();
                IndexByNodeGuid[nodeId] = temp;
            }
            return temp;
        }

        [NonSerialized]
        private Dictionary<Guid, CUserList> _indexByNodeGuid;
        private Dictionary<Guid, CUserList> IndexByNodeGuid
        {
            get
            {
                if (null == _indexByNodeGuid)
                {
                    Dictionary<Guid, CUserList> index = new Dictionary<Guid, CUserList>();
                    CUserList temp = null;
                    foreach (CUser i in this)
                    {
                        if (! index.TryGetValue(i.UserNodeGuid, out temp))
                        {
                            temp = new CUserList();
                            index[i.UserNodeGuid] = temp;
                        }
                        temp.Add(i);
                    }
                    _indexByNodeGuid = index;
                }
                return _indexByNodeGuid;
            }
        }
        //Index by UserTypeId
        public CUserList GetByTypeId(int typeId)
        {
            CUserList temp = null;
            if (! IndexByTypeId.TryGetValue(typeId, out temp))
            {
                temp = new CUserList();
                IndexByTypeId[typeId] = temp;
            }
            return temp;
        }

        [NonSerialized]
        private Dictionary<int, CUserList> _indexByTypeId;
        private Dictionary<int, CUserList> IndexByTypeId
        {
            get
            {
                if (null == _indexByTypeId)
                {
                    Dictionary<int, CUserList> index = new Dictionary<int, CUserList>();
                    CUserList temp = null;
                    foreach (CUser i in this)
                    {
                        if (! index.TryGetValue(i.UserTypeId, out temp))
                        {
                            temp = new CUserList();
                            index[i.UserTypeId] = temp;
                        }
                        temp.Add(i);
                    }
                    _indexByTypeId = index;
                }
                return _indexByTypeId;
            }
        }
        //Index by UserIsActive
        public CUserList GetByIsActive(bool isActive)
        {
            CUserList temp = null;
            if (! IndexByIsActive.TryGetValue(isActive, out temp))
            {
                temp = new CUserList();
                IndexByIsActive[isActive] = temp;
            }
            return temp;
        }

        [NonSerialized]
        private Dictionary<bool, CUserList> _indexByIsActive;
        private Dictionary<bool, CUserList> IndexByIsActive
        {
            get
            {
                if (null == _indexByIsActive)
                {
                    Dictionary<bool, CUserList> index = new Dictionary<bool, CUserList>();
                    CUserList temp = null;
                    foreach (CUser i in this)
                    {
                        if (! index.TryGetValue(i.UserIsActive, out temp))
                        {
                            temp = new CUserList();
                            index[i.UserIsActive] = temp;
                        }
                        temp.Add(i);
                    }
                    _indexByIsActive = index;
                }
                return _indexByIsActive;
            }
        }
        //Index by UserName
        public CUserList GetByName(string name)
        {
            CUserList temp = null;
            if (! IndexByName.TryGetValue(name, out temp))
            {
                temp = new CUserList();
                IndexByName[name] = temp;
            }
            return temp;
        }

        [NonSerialized]
        private Dictionary<string, CUserList> _indexByName;
        private Dictionary<string, CUserList> IndexByName
        {
            get
            {
                if (null == _indexByName)
                {
                    Dictionary<string, CUserList> index = new Dictionary<string, CUserList>();
                    CUserList temp = null;
                    foreach (CUser i in this)
                    {
                        if (! index.TryGetValue(i.UserName, out temp))
                        {
                            temp = new CUserList();
                            index[i.UserName] = temp;
                        }
                        temp.Add(i);
                    }
                    _indexByName = index;
                }
                return _indexByName;
            }
        }
        //Index by UserTokenHasExpired
        public CUserList GetByTokenHasExpired(bool tokenHasExpired)
        {
            CUserList temp = null;
            if (! IndexByTokenHasExpired.TryGetValue(tokenHasExpired, out temp))
            {
                temp = new CUserList();
                IndexByTokenHasExpired[tokenHasExpired] = temp;
            }
            return temp;
        }

        [NonSerialized]
        private Dictionary<bool, CUserList> _indexByTokenHasExpired;
        private Dictionary<bool, CUserList> IndexByTokenHasExpired
        {
            get
            {
                if (null == _indexByTokenHasExpired)
                {
                    Dictionary<bool, CUserList> index = new Dictionary<bool, CUserList>();
                    CUserList temp = null;
                    foreach (CUser i in this)
                    {
                        if (! index.TryGetValue(i.UserTokenHasExpired, out temp))
                        {
                            temp = new CUserList();
                            index[i.UserTokenHasExpired] = temp;
                        }
                        temp.Add(i);
                    }
                    _indexByTokenHasExpired = index;
                }
                return _indexByTokenHasExpired;
            }
        }
        #endregion

    }
}
